//
//  MyPointsListVc.m
//  BaseProject
//
//  Created by 王盼盼 on 2024/11/7.
//  Copyright © 2024 wangpanpan. All rights reserved.
//

#import "MyPointsListVc.h"
#import "MyPointsListViewModel.h"
#import "YunOrderListModel.h"
#import "MyPointsListCell.h"
#import "MyPointsTableHeader.h"
#import "MyPointsHeadCard.h"
#import "MyPointsMask.h"
@interface MyPointsListVc ()<UITableViewDelegate, UITableViewDataSource>
@property(nonatomic, strong) MyPointsListViewModel *viewModel;
@property (nonatomic, strong)UITableView *tableview;
@property(nonatomic, strong) MyPointsHeadCard *headCard;
@property(nonatomic, strong) MyPointsTableHeader *tableHeader;
@end

@implementation MyPointsListVc
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"积分列表";
}
- (void)makeUI {
    [super makeUI];
    [self.view addSubview:self.tableview];
    [self.view addSubview:self.headCard];
    [self.view addSubview:self.tableHeader];
    [self.headCard mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(12);
        make.right.mas_equalTo(-12);
        make.height.mas_equalTo(80*ADAPTER_WIDTH);
    }];
    [self.tableHeader mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(self.headCard.mas_bottom).mas_equalTo(12);
        make.height.mas_equalTo(48);
    }];
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.mas_equalTo(self.tableHeader.mas_bottom).mas_equalTo(0);
    }];
}
- (void)handleEvent {
    [super handleEvent];
    WK(weakSelf)
    self.tableview.mj_header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        [weakSelf.viewModel refresh:YES];
    }];
    self.tableview.mj_footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{
        [weakSelf.viewModel refresh:NO];
    }];
    [self.viewModel setRefresh:^(BOOL hasNext) {
        [weakSelf.tableview reloadData];
        [weakSelf.tableview.mj_header endRefreshing];
        [weakSelf.tableview.mj_footer endRefreshing];
        if (weakSelf.viewModel.dataArray.count == 0) {
            [[TTNoResultManager shareManager] showNoResultWithStateInView:weakSelf.tableview errorCode:kTTViewState_NoOrder btnTitle:@"" didPressButton:^{}];
        } else {
            [[TTNoResultManager shareManager] hideNoResultWithView:weakSelf.tableview];
        }
        if (hasNext) {
            [weakSelf.tableview.mj_footer resetNoMoreData];
        } else {
            [weakSelf.tableview.mj_footer endRefreshingWithNoMoreData];
        }
    }];
}
- (void)bindViewModel {
    [super bindViewModel];
    [self.viewModel transfrom];
    self.headCard.countModel = self.countModel;
}
#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.viewModel.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyPointsListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    cell.data = self.viewModel.dataArray[indexPath.row];
    cell.lineView.hidden = indexPath.row == 0;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.viewModel.dataArray[indexPath.row].cellHeight;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}
#pragma mark - click
- (void)clickShuoMingImageView:(CGRect)imageViewFrame {
    CGRect relFrame = [self.view convertRect:imageViewFrame fromView:self.headCard];
    MyPointsMask *mask = [[MyPointsMask alloc]initWithFrame:self.view.bounds];
    [self.view addSubview:mask];
    mask.tipPoint = CGPointMake(CGRectGetMidX(relFrame), CGRectGetMaxY(relFrame));
}
#pragma mark - lazy
- (MyPointsListViewModel *)viewModel {
    if (!_viewModel) {
        _viewModel = [[MyPointsListViewModel alloc]init];
    }
    return _viewModel;
}
- (UITableView *)tableview {
    if (!_tableview) {
        UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:UITableViewStylePlain];
        [self.view addSubview:tableView];
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.tableFooterView = [UIView new];
        tableView.tableHeaderView = [UIView new];
        [tableView registerClass:[MyPointsListCell class] forCellReuseIdentifier:@"Cell"];
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.backgroundColor = UIColorHex(0xf1f1f1);
        tableView.rowHeight = 50;
        if (@available(iOS 11.0, *)) {
            tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            tableView.estimatedRowHeight = 0;
            tableView.estimatedSectionFooterHeight = 0;
            tableView.estimatedSectionHeaderHeight = 0;
        }
        if (@available(ios 15.0,*)) {
            UITableView.appearance.sectionHeaderTopPadding = 0;
        }
        _tableview = tableView;
    }
    return _tableview;
}
- (MyPointsHeadCard *)headCard {
    if (!_headCard) {
        _headCard = [[MyPointsHeadCard alloc]initWithFrame:CGRectMake(12, 12, kScreenWidth - 24, 80 * ADAPTER_WIDTH)];
        WK(weakSelf)
        [_headCard setClickImageViewCallBackImageViewFrame:^(CGRect frame) {
            [weakSelf clickShuoMingImageView:frame];
        }];
    }
    return _headCard;
}
- (MyPointsTableHeader *)tableHeader {
    if (!_tableHeader) {
        _tableHeader = [[MyPointsTableHeader alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 48)];
    }
    return _tableHeader;
}
@end
